﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Logic.BaseContent;
using System.Data;
using Job.Web.Framework;
using Job.Logic.Common;
using Job.Entity;

namespace Job.Web.userControls.Hunter
{
    public partial class HunterJobList : BaseCtrl
    {
        HunterJobLogic logic = new HunterJobLogic();
        JobHunter searcher = new JobHunter();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                searcher.Name = myR["name"].StringValue;
                searcher.SearchKey = myR["key"].StringValue;
                searcher.Salary = myR["salary"].StringValue;

                string cacheKey = myR["CK"].StringValue;
                if (null != cacheKey && cacheKey.Length > 0)
                {
                    searcher = (JobHunter)Cache[cacheKey];
                    Cache.Remove(cacheKey);
                    if (null == searcher) searcher = new JobHunter();
                }


                BindCount();
                BindData();
                LoadControl();
            }
        }


        private void LoadControl()
        {
            tbJobName.Text = searcher.Name;
            tbSearchKey.Text = searcher.SearchKey;
            
            string salary = string.Empty;
            if (searcher.Salary != null) salary = searcher.Salary.Trim(); 
             WebCommon.SetDropDownFromDict(ddlYearSalary, salary,
                Constants.GetDictionaryCode(Constants.DictionaryType.SALARY_LEVEL), true);

            string addr = searcher.OfficeAddress;
            addr = (null == addr) ? "" : addr.Trim();
            WebCommon.LoadLevelDropDownList(ddlAddrFather, ddlAddrSub, AddressRate,
                Constants.GetParentCodeBySubCode(addr),
                addr, Constants.DictionaryType.ADDR_CITY);
            string industry = searcher.Industry;
            industry = (null == industry) ? "" : industry.Trim();
            WebCommon.LoadLevelDropDownList(ddlIndustryFather, ddlIndustrySub, IndustryRelate,
                Constants.GetParentCodeBySubCode(industry),
                industry, Constants.DictionaryType.INDUSTRY_CLASS);
        }

        private void BindCount()
        {
            int count = logic.GetCount(searcher);
            if (count <= pagerTop.PageSize)
            {
                pagerTop.Visible = false;
                pagerButtom.Visible = false;
            }
            else
            {
                pagerButtom.RecordCount = count;
                pagerButtom.DataBind();
                pagerTop.RecordCount = count;
                pagerTop.DataBind();
            }
        }

        private void BindData()
        {
            int pageSize = pagerButtom.PageSize;
            int pageIdx = pagerButtom.CurrentPageIndex;

            DataTable dt = logic.GetJobPageList(pageSize, pageIdx, searcher);
            if ( null != dt )
            {
                this.jobList.DataSource = dt;
                this.jobList.DataBind();
            }
        }



        protected void pagerTop_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void pagerTop_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pagerButtom.CurrentPageIndex = e.NewPageIndex;
            Session["listcurrentPageIndex"] = e.NewPageIndex;
        }

        protected void pagerButtom_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void pagerButtom_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            pagerTop.CurrentPageIndex = e.NewPageIndex;
            Session["listcurrentPageIndex"] = e.NewPageIndex;
        }

        protected void jobList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink jobDetail = (HyperLink)e.Item.FindControl("jobLink");
                        Label lbJobNo = (Label)e.Item.FindControl("jobNo");

                        jobDetail.NavigateUrl = WebCommon.GetApplicationPath(Request)  + "Item.aspx?categoryid="
                            + ((int)Constants.CategoryType.Hunter).ToString()
                            + "&subjectid=" + ((int)Constants.SubjectType.HunterPositionList).ToString()
                            + "&jobno=" + lbJobNo.Text.Trim();

                        Label lbSalay = (Label)e.Item.FindControl("lbSalary");
                        if ( null != lbSalay ) lbSalay.Text = MemoryDictionary.GetCommonDictName(lbSalay.Text);
                        Label lbIndustry = (Label)e.Item.FindControl("lbIndustry");
                        if ( null != lbIndustry) lbIndustry.Text = MemoryDictionary.GetIndustryName(lbIndustry.Text);
                        break;
                    }
            }            

        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            searcher = new JobHunter();
            searcher.Name = tbJobName.Text.Trim();
            searcher.Salary = ddlYearSalary.SelectedValue;
            searcher.SearchKey = tbSearchKey.Text.Trim();
            if (ddlAddrSub.SelectedValue != null && ddlAddrSub.SelectedValue.Length > 1)
                searcher.OfficeAddress = ddlAddrSub.SelectedValue;
            else if (ddlAddrFather.SelectedValue != null && ddlAddrFather.SelectedValue.Length > 1)
                searcher.OfficeAddress = ddlAddrFather.SelectedValue;

            if (ddlIndustrySub.SelectedValue != null && ddlIndustrySub.SelectedValue.Length > 1)
                searcher.Industry = ddlIndustrySub.SelectedValue;
            else if (ddlIndustryFather.SelectedValue != null && ddlIndustryFather.SelectedValue.Length > 1)
                searcher.Industry = ddlIndustryFather.SelectedValue;

            BindCount();
            BindData();
        }

    }
}